#!/bin/sh

# This script is a cron job which is called daily to:
#  - sort and decorate the master lists
#  - commit the master lists to notabug.org/mandy/lists
#  - update youtube-dl
#  - email a health report of basic statistics and errors or warnings

master_lists="common/master_lists"
public_lists="common/public_lists"
errorReporter="common/report/errorReporter.sh"
reportSender="common/report/reportSender.sh"


# create public sorted lists from the master lists
for list in $public_lists/*
do
    # isolate filename
    list_name=`echo $list | sed "s/.*\///"`
    # safely remove duplicates, sort list
    flock $master_lists/$list_name -c "sort $master_lists/$list_name | 
        uniq > $public_lists/$list_name"
done

# git commit and push public lists
git -C $public_lists commit -a -m "(updated daily)"
git -C $public_lists push

# update youtube-dl
pip3 install --upgrade --user youtube-dl

# safely compile report from log reports and send
flock $errorReporter -c "sh $reportSender"
